package cn.edu.mju.property.dao.impl;

import cn.edu.mju.property.dao.UnitDao;
import cn.edu.mju.property.entity.Notice;
import cn.edu.mju.property.entity.Unit;
import cn.edu.mju.property.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UnitDaoImpl implements UnitDao {
    @Override
    public boolean addUnit(Unit unit) {
        return false;
    }

    @Override
    public boolean delUnit(int id) {
        return false;
    }

    @Override
    public boolean updateUnit(Unit unit) {
        return false;
    }

    @Override
    public List<Unit> findUnitByPage(int start, int rows) {
        Unit unit = null;
        List<Unit> Unitlist = new ArrayList<Unit>();
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from unit limit ? , ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,start);
            pstmt.setInt(2,rows);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                unit = new Unit();
                unit.setId(rs.getInt("id"));
                unit.setName(rs.getString("name"));
                unit.setBuilding(rs.getString("building"));
                Unitlist.add(unit);
            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Unitlist;
    }

    @Override
    public int findTotalCount() {


        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select count(*) from unit";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            int num =  rs.getInt("count(*)");
            pstmt.close();
            conn.close();
            return num;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }
}
